<?php

namespace frontend\controllers;

use Yii;
use common\models\News;
use common\models\search\NewsSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use frontend\controllers\base\BaseController;
use common\models\NewColumn;
use yii\data\Pagination;

/**
 * NewsController implements the CRUD actions for News model.
 */
class NewsController extends BaseController
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * Lists all News models.
     * @return mixed
     */
    public function actionIndex()
    {
        $column=NewColumn::find()->asArray()->all();
        $new=News::find()->orderBy('id desc')->limit(7)->asArray()->all();
        $hot=News::find()->orderBy('look desc')->limit(7)->asArray()->all();
        $data=News::find()->where('(select count(*) from `news` n where n.pid=`news`.pid and n.id>`news`.id)<6')->asArray()->all();
        $data=$this->dataSort($data);
        $this->columnData();
        $this->showCart();
        return $this->render('index', [
            'column'=>$column,
            'data'=>$data,
            'new'=>$new,
            'hot'=>$hot
        ]);
    }
    public function actionList($id)
    {
        $column=NewColumn::find()->asArray()->all();
        if(!empty($column)){
            $column=array_column($column, null,'id');
        }
        $this->columnData();
        $this->showCart();
        $cdata=News::find()->where('(select count(*) from `news` n where n.pid=`news`.pid and n.id>`news`.id)<6 and pid !='.$id)->orderBy('id desc')->asArray()->all();
        $cdata=$this->dataSort($cdata);
        $model=News::find()->where('pid='.$id);
        $pages=new Pagination(['totalCount'=>$model->count(),'pageSize'=>10]);
        $data=$model->offset($pages->offset)->limit($pages->limit)->orderBy('id desc')->asArray()->all();
        return $this->render('list', [
            'data'=>$data,
            'cdata'=>$cdata,
            'column'=>$column,
            'pages'=>$pages
        ]);
    }
    /**
     * Displays a single News model.
     * @param string $id
     * @return mixed
     */
    public function actionView($id)
    {
        $model=$this->findModel($id);
        if($model){
            $model->look+=1;
            $model->save(false);
        }
        $prev=News::find()->select('id,title')->where('id<'.$model->id)->one();
        $next=News::find()->select('id,title')->where('id>'.$model->id)->one();
        $top=News::find()->select('id,title,createtime')->where("id!={$id} and pid={$model->pid}")->limit(10)->orderBy('look desc')->asArray()->all();
        $hot=News::find()->select('id,title,createtime')->orderBy('look desc')->limit(8)->asArray()->all();
        $column=NewColumn::find()->asArray()->all();
        $this->columnData();
        $this->showCart();
        if(!empty($column)){
            $column=array_column($column, null,'id');
        }
        return $this->render('view', [
            'model' => $model,
            'prev'=>$prev,
            'next'=>$next,
            'top'=>$top,
            'hot'=>$hot,
            'column'=>$column
        ]);
    }

   

    /**
     * Finds the News model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param string $id
     * @return News the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = News::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
}
